package com.ddxz.demo.flowable.service.impl;

import com.ddxz.demo.flowable.service.DdxzModelService;
import com.ddxz.tool.core.convert.StringUtil;
import com.ddxz.tool.web.dto.DdxzResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.model.ModelRepresentation;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/12/14
 */

@Service
public class DdxzModelServiceImpl implements DdxzModelService {
    @Override
    public DdxzResponse getModelById(String modelId) {
        Model model = modelService.getModel(modelId);
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("modelId", modelId);
        objectNode.put("name", model.getName());
        objectNode.put("key", model.getKey());
        objectNode.put("description", model.getDescription());
        objectNode.put("lastUpdatedBy", model.getLastUpdatedBy());
        objectNode.putPOJO("lastUpdated", model.getLastUpdated());

        if(StringUtil.isEmpty(model.getModelEditorJson())) {
            // 刚创建的模型，完成初始化
            ObjectNode newObjectNode = objectMapper.createObjectNode();
            newObjectNode.put("id", "canvas");
            newObjectNode.put("resourceId", "canvas");
            ObjectNode stencilSetNode = objectMapper.createObjectNode();
            stencilSetNode.putPOJO("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
            newObjectNode.put("modelType", "model");
            newObjectNode.putPOJO("stencilset", stencilSetNode);
            objectNode.set("model", newObjectNode);
        } else {
            // 加载之前的模型值
            try {
                ObjectNode editorJsonNode = (ObjectNode) objectMapper.readTree(model.getModelEditorJson());
                editorJsonNode.put("modelType", "model");
                objectNode.set("model", editorJsonNode);
            } catch (Exception e) {
                throw new RuntimeException("Error reading editor json " + modelId, e);
            }
        }
        return DdxzResponse.correct(objectNode);
    }

    @Override
    public void saveModel(org.flowable.engine.repository.Model model) {
        ModelRepresentation entity = new ModelRepresentation();
        entity.setId(model.getId());
        entity.setKey(model.getKey());
        entity.setDescription(model.getCategory());
//        entity.setCreated(model.getCreateTime());
        entity.setVersion(model.getVersion());
        entity.setTenantId(model.getTenantId());
        entity.setModelType(0);
        entity.setName(model.getName());
        modelService.createModel(entity, modelService.createModelJson(entity), USER);
//        modelService.saveModel(entity);
    }

    @Autowired
    private ModelService modelService;

    @Autowired
    private ObjectMapper objectMapper;
}
